var e = require("../../requests/request.js"), a = (require("../../utils/util.js"), 
require("../../requests/newapi.js")), t = a.postEnquireOrder, i = a.decryptUserInfo, s = getApp();

Page({
    data: {
        carid: 0,
        cityid: 0,
        img: "",
        carname: "",
        cityname: "",
        dealerlist: [],
        dealerInfoList: [],
        dealercount: 0,
        serialid: 0,
        username: "",
        usertel: "",
        errmsghidden: !0,
        errmsg: "",
        layshow: !0,
        subordertype: 0,
        isflag: !1,
        phoneauthmsg: "",
        btntxt: "获取底价"
    },
    onLoad: function(e) {
        var a = this;
        a.data.img = decodeURIComponent(e.img), a.data.carname = decodeURIComponent(e.carname), 
        a.data.carid = e.carid, a.data.cityid = e.cityid, a.data.cityname = decodeURIComponent(e.cityname), 
        a.data.serialid = e.serialid, a.data.subordertype = e.subordertype, 0 == a.data.subordertype ? wx.setNavigationBarTitle({
            title: "询底价"
        }) : (wx.setNavigationBarTitle({
            title: "贷款"
        }), a.setData({
            btntxt: "申请贷款"
        })), this.setData({
            img: decodeURIComponent(e.img),
            carid: e.carid,
            cityid: e.cityid,
            carname: decodeURIComponent(e.carname),
            cityname: decodeURIComponent(e.cityname),
            serialid: e.serialid
        }), a.setDealerData();
    },
    onCallBackCity: function(e) {
        this.city.hideCity(), this.setData({
            cityid: e.detail.id,
            cityname: e.detail.name,
            layshow: !this.data.layshow
        }), this.setDealerData();
    },
    onReady: function() {
        try {
            this.city = this.selectComponent("#city"), this.cardetail = this.selectComponent("#cardetail");
        } catch (e) {
            this.data.isflag = !0;
        }
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    setDealerData: function() {
        var a = this;
        wx.showLoading(), e.getDealerInfoList(a.data.carid, a.data.cityid, function(e) {
            a.successCallback(e);
        }, function(e) {}, function(e) {
            wx.hideLoading();
        });
    },
    successCallback: function(e) {
        var a = this, t = e.Data;
        a.data.dealerInfoList = t.List, a.dealerlist = new Array();
        var i = 0;
        for (var s in a.data.dealerInfoList) {
            var n = "4S店" === a.data.dealerInfoList[s].DealerBizModeName;
            if (a.data.dealerInfoList[s].Is4S = n, void 0 != a.data.dealerInfoList[s].DealerBizModeName && a.data.dealerInfoList[s].DealerBizModeName.length > 2 && (a.data.dealerInfoList[s].DealerBizModeName = a.data.dealerInfoList[s].DealerBizModeName.substring(0, 2), 
            a.data.dealerInfoList[s].DealerTel = a.data.dealerInfoList[s].DealerTel.replace(/转/, ","), 
            a.data.dealerInfoList[s].DealerTelShow = a.data.dealerInfoList[s].DealerTel.split(",")[0]), 
            void 0 != a.data.dealerInfoList[s].DealerID) {
                var r = new Object();
                r.Index = i, r.DealerID = a.data.dealerInfoList[s].DealerID, r.DealerBizModeName = a.data.dealerInfoList[s].DealerBizModeName, 
                r.DealerName = a.data.dealerInfoList[s].DealerName, r.SaleRegion = a.data.dealerInfoList[s].SaleRegion, 
                r.DealerSaleAddr = a.data.dealerInfoList[s].DealerSaleAddr, r.DealerTel = a.data.dealerInfoList[s].DealerTel, 
                r.SaleRegion = a.data.dealerInfoList[s].SaleRegion, r.IsSelect = i < 3, a.dealerlist[i] = r, 
                i++;
            }
        }
        a.setData({
            dealerInfoList: a.data.dealerInfoList,
            dealerlist: a.dealerlist,
            dealercount: a.dealerlist.length
        });
    },
    bindlistclick: function(e) {
        var a = this, t = e.currentTarget, i = parseInt(t.dataset.id), s = a.dealerlist;
        s[i].IsSelect = !s[i].IsSelect, a.setData({
            dealerlist: s
        });
    },
    cityclick: function(e) {
        this.data.isflag ? wx.showModal({
            title: " 提示",
            content: "您微信版本过低，无法切换城市"
        }) : (this.setData({
            layshow: !this.data.layshow
        }), this.city.showCity());
    },
    serialcarclick: function(e) {
        if (this.data.isflag) wx.showModal({
            title: " 提示",
            content: "您微信版本过低，无法切换车款"
        }); else {
            this.setData({
                layshow: !this.data.layshow
            });
            var a = {
                serialid: e.currentTarget.dataset.serialid
            };
            this.cardetail.showCar(a);
        }
    },
    onCallBackCarDetail: function(e) {
        this.cardetail.hideCar(), this.setData({
            carid: e.detail.carid,
            carname: e.detail.showname + " " + e.detail.carname,
            img: e.detail.img,
            layshow: !this.data.layshow
        }), this.setDealerData();
    },
    bindUserNameInput: function(e) {
        this.setData({
            username: e.detail.value,
            errmsghidden: !0
        });
    },
    bindUserTelInput: function(e) {
        this.setData({
            usertel: e.detail.value,
            errmsghidden: !0
        });
    },
    getPhoneNumber: function(e) {
        var a = this, t = e.detail, s = t.iv, n = t.encryptedData, r = wx.getStorageSync("session_key");
        s && n ? (wx.showLoading(), i({
            op: "encrypteddata",
            iv: s,
            encrypteddatastr: n,
            sessionkey: r
        }).then(function(e) {
            wx.hideLoading(), a.setData({
                usertel: e.phoneNumber
            });
        }).catch(function(e) {
            wx.showToast({
                title: "获取失败",
                icon: "none"
            }), a.setData({
                phoneauthmsg: "deny"
            });
        })) : this.setData({
            phoneauthmsg: "deny"
        });
    },
    telDealer: function(e) {
        wx.showModal({
            title: "拨打电话",
            content: "电话号码：" + e.currentTarget.dataset.tel,
            success: function(a) {
                a.confirm && wx.makePhoneCall({
                    phoneNumber: e.currentTarget.dataset.tel
                });
            }
        });
    },
    bindPostOrder: function() {
        var e = this, a = this, i = "", s = wx.getStorageSync("UserToken"), n = 0;
        for (var r in a.data.dealerlist) a.data.dealerlist[r].IsSelect && (i += a.data.dealerlist[r].DealerID + ",", 
        n++);
        n < 1 ? a.setmsg("至少选择一个经销商询价") : n > 5 ? a.setmsg("最多只能选5家经销商询价") : (i = i.substr(0, i.length - 1), 
        a.checkusername(a.data.username) && a.checkPhone(a.data.usertel) && (wx.showLoading(), 
        t({
            carid: this.data.carid,
            cityid: this.data.cityid,
            dealerids: i,
            username: this.data.username,
            usertel: this.data.usertel,
            subordertype: this.data.subordertype,
            token: s
        }).then(function(a) {
            Array.isArray(a) ? (wx.showToast({
                title: "提交成功",
                mask: !0
            }), setTimeout(wx.navigateBack, 1500)) : (wx.hideLoading(), e.setData({
                errmsghidden: !1,
                errmsg: a.Message
            }));
        }).catch(function(a) {
            wx.hideLoading(), e.setmsg("提交失败，请稍等再试");
        })));
    },
    setmsg: function(e) {
        var a = this;
        a.setData({
            errmsg: e,
            errmsghidden: !1
        }), setTimeout(function() {
            a.setData({
                errmsghidden: !0
            });
        }, 2e3);
    },
    checkusername: function(e) {
        var a = this;
        return !!/^[\一-\龥]{1,6}$/.test(e) || (a.setmsg("姓名要求1-6个汉字"), !1);
    },
    checkPhone: function(e) {
        var a = this;
        return !!/^1[34578]\d{9}$/.test(e) || (a.setmsg("手机号填写错误"), !1);
    },
    returnBtn: function() {
        s.gotoIndex();
    }
});